package com.slz3.business.module.goodsManagement.baseGoods.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.baseGoods.domain.BaseGoods;
import com.slz3.business.module.goodsManagement.baseGoods.domain.BaseGoodsFactory;
import com.slz3.business.module.goodsManagement.baseGoods.steward.BaseGoodsSpecification;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttribute.domain.GooBaseGoodsAttribute;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttributeValue.domain.GooBaseGoodsAttributeValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class BaseGoodsServiceImpl implements BaseGoodsService {
    @Override
    public BaseGoods save(BaseGoods baseGoods) {
        return Repositorys.baseGoods.save(BaseGoodsFactory.initBaseFields(baseGoods));
    }
    @Override
    public Page<BaseGoods> page(MultiValueMap<String, String> params) {
        Page<BaseGoods> baseGoodsPage = Repositorys.baseGoods.findAll(
                BaseGoodsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return baseGoodsPage;
    }


    @Override
    public BaseGoods modify(BaseGoods baseGoodsNew) {
        return Repositorys.baseGoods.saveAndFlush(BaseGoodsFactory.initBaseFields(baseGoodsNew));
    }

    @Override
    public Optional<BaseGoods> single(UUID uuid) {
        Optional<BaseGoods> baseGoods = Repositorys.baseGoods.findById(uuid);
        if(baseGoods.isPresent()){
            BaseGoods goods = baseGoods.get();
            UUID baseGoodsUUID = goods.getUuid();
            GooBaseGoodsAttribute attribute = new GooBaseGoodsAttribute();
            attribute.setFkBaseGoodsUUID(baseGoodsUUID);
            Example<GooBaseGoodsAttribute> attributeExample = Example.of(attribute);
            List<GooBaseGoodsAttribute> attributes = Repositorys.gooBaseGoodsAttribute.findAll(attributeExample);
            attributes.forEach(item -> {
                UUID attributeUUID = item.getUuid();
                GooBaseGoodsAttributeValue attributeValue = new GooBaseGoodsAttributeValue();
                attributeValue.setUpper(attributeUUID);
                Example<GooBaseGoodsAttributeValue> attributeValueExample = Example.of(attributeValue);
                List<GooBaseGoodsAttributeValue> attributeValues = Repositorys.gooBaseGoodsAttributeValue.findAll(attributeValueExample);
                item.setGooBaseGoodsAttributeValueList(attributeValues);
            });
            goods.setGooBaseGoodsAttributeList(attributes);
        }
        return baseGoods;
    }

    @Override
    public List<BaseGoods> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.baseGoods.findAllById(uuids);
    }

    @Override
    public List<BaseGoods> delete(List<BaseGoods> baseGoodsList) {
        baseGoodsList.forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            BaseGoodsFactory.initBaseFields(item);
        });
        return Services.baseGoodsTransactional.batchModify(baseGoodsList);
    }

}
